from collections import deque
import concurrent
import time
from collections import deque
from concurrent.futures import ThreadPoolExecutor
from datetime import datetime

import requests

from apps.zhcxkj_center_data.prod.views.get_active_source_id import Common
from common.setting import ensure_path_sep
from third_services.aliexpress.api.api import AliexpressApi
from third_services.aliexpress.utils import ErpDesUtil
from utils.mysqlUtils.db_pool import Database
from utils.otherutils.MyThreadPool import MyThreadPool


class TestCategory:
    # ...（其他代码不变）

    def get_all_categories(self, category_id):
        max_global_retries = 20  # 全局最大重试次数
        global_retries = 0  # 全局重试计数器

        category_id_rst = self.fetch_category_children(category_id)
        category_id_list = category_id_rst[0]
        visited = set()
        queue = deque(category_id_list)  # 使用deque优化队列操作
        all_categories = []
        leaves = []

        while queue:
            if global_retries >= max_global_retries:
                raise Exception("全局重试次数超限")

            current_id = queue.popleft()
            if current_id in visited:
                continue
            visited.add(current_id)
            all_categories.append(current_id)

            retries = 0
            while retries < 5:
                try:
                    children = self.fetch_category_children(current_id)
                    if children:
                        for child_id in children[0]:
                            queue.append(child_id)
                        for leaf_id in children[1]:
                            leaves.append(leaf_id)
                        break  # 成功获取子节点，跳出重试循环
                except Exception as e:
                    print(f"获取子节点时发生异常：{e}")
                    retries += 1
                    global_retries += 1  # 增加全局重试计数器
                else:
                    retries = -1  # 成功获取，将retries设为-1以跳出while循环

                if retries >= 5:
                    print(f'类目：{current_id} 获取子节点失败，已达到最大重试次数。')
                    global_retries += 1  # 仅在重试失败后增加全局计数器

        return all_categories, leaves